/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuPrincipal.java
 *
 * Created on Nov 17, 2010, 10:20:35 AM
 */
package br.com.tbp.gui;

import br.com.tbp.model.domain.Usuario;
import br.com.tbp.utils.DateUtils;
import br.com.tbp.utils.ImageUtils;
import com.towel.swing.img.JImagePanel;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.UIManager;
import org.jdesktop.swingx.JXHyperlink;

/**
 *
 * @author Diego Freire
 */
public class MenuConsultor extends javax.swing.JFrame {

    private JImagePanel imagem_rodape;
    private LoginDialog loginDialog;
    private LogoutAction logoutAction;

    public MenuConsultor(LoginDialog login) {
        initComponents();
        setSize(1024, 700);
        setLocationRelativeTo(null);
        this.loginDialog = login;
        setIconImage(ImageUtils.createIcon("sistema.png").getImage());
        carregaCabecalho();
        carregaRodape();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usuarioPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        bemVindoLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        dataLabel = new javax.swing.JLabel();
        logoutPanel = new javax.swing.JPanel();
        artePanel = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        acrielLabel = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        arquivoMenu = new javax.swing.JMenu();
        ferramentasMenu = new javax.swing.JMenu();
        calculadoraMenuItem = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JPopupMenu.Separator();
        logoutMenuItem = new javax.swing.JMenuItem();
        sairMenuItem = new javax.swing.JMenuItem();
        cadastroMenu = new javax.swing.JMenu();
        pesquisarPedidoMenuItem = new javax.swing.JMenuItem();
        funilMenu = new javax.swing.JMenu();
        novaVisitaMenuItem = new javax.swing.JMenuItem();
        pesquisarVisitaMenuItem = new javax.swing.JMenuItem();
        ajudaMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Acriel Consultoria - Agente Business");

        usuarioPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(195, 223, 251)));
        usuarioPanel.setLayout(new java.awt.GridLayout(3, 0));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        bemVindoLabel.setText("Bem-vindo Usuário, seu último acesso foi em: 18/01/2011 20:50");
        jPanel1.add(bemVindoLabel);

        usuarioPanel.add(jPanel1);

        jPanel2.setBackground(new java.awt.Color(195, 223, 251));
        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        dataLabel.setText("Hoje é Terça-feira, 18/01/2011");
        jPanel2.add(dataLabel);

        usuarioPanel.add(jPanel2);

        logoutPanel.setBackground(new java.awt.Color(255, 255, 255));
        logoutPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
        usuarioPanel.add(logoutPanel);

        getContentPane().add(usuarioPanel, java.awt.BorderLayout.PAGE_START);

        artePanel.setPreferredSize(new java.awt.Dimension(1024, 112));
        artePanel.setLayout(new java.awt.BorderLayout());
        getContentPane().add(artePanel, java.awt.BorderLayout.PAGE_END);

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        acrielLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/acriel-marca.png"))); // NOI18N
        acrielLabel.setToolTipText("www.acrielconsultoria.com.br");
        acrielLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        acrielLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                acrielLabelMouseClicked(evt);
            }
        });
        jPanel5.add(acrielLabel);

        jPanel4.add(jPanel5, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);

        arquivoMenu.setText("Arquivo");

        ferramentasMenu.setText("Ferramentas");

        calculadoraMenuItem.setText("Calculadora");
        calculadoraMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculadoraMenuItemActionPerformed(evt);
            }
        });
        ferramentasMenu.add(calculadoraMenuItem);

        arquivoMenu.add(ferramentasMenu);
        arquivoMenu.add(jSeparator6);

        logoutMenuItem.setText("Logout");
        arquivoMenu.add(logoutMenuItem);

        sairMenuItem.setText("Sair");
        sairMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sairMenuItemActionPerformed(evt);
            }
        });
        arquivoMenu.add(sairMenuItem);

        menuBar.add(arquivoMenu);

        cadastroMenu.setText("Meus Pedidos");

        pesquisarPedidoMenuItem.setText("Pesquisar");
        pesquisarPedidoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F6, 0));
        pesquisarPedidoMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/vendas16.png"))); // NOI18N
        pesquisarPedidoMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarPedidoMenuItemActionPerformed(evt);
            }
        });
        cadastroMenu.add(pesquisarPedidoMenuItem);

        menuBar.add(cadastroMenu);

        funilMenu.setText("Funil de Vendas");

        novaVisitaMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/funil16x16.png"))); // NOI18N
        novaVisitaMenuItem.setText("Nova Visita");
        novaVisitaMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novaVisitaMenuItemActionPerformed(evt);
            }
        });
        funilMenu.add(novaVisitaMenuItem);

        pesquisarVisitaMenuItem.setText("Pesquisar Visita");
        pesquisarVisitaMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        pesquisarVisitaMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarVisitaMenuItemActionPerformed(evt);
            }
        });
        funilMenu.add(pesquisarVisitaMenuItem);

        menuBar.add(funilMenu);

        ajudaMenu.setText("Ajuda");

        aboutMenuItem.setText("Sobre");
        aboutMenuItem.setEnabled(false);
        ajudaMenu.add(aboutMenuItem);

        menuBar.add(ajudaMenu);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void calculadoraMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calculadoraMenuItemActionPerformed
        try {
            Runtime.getRuntime().exec("calc.exe");
        } catch (IOException ioex) {
            return;
        }
}//GEN-LAST:event_calculadoraMenuItemActionPerformed

    private void sairMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sairMenuItemActionPerformed
        System.exit(0);
}//GEN-LAST:event_sairMenuItemActionPerformed

    private void acrielLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_acrielLabelMouseClicked
        String acriel = acrielLabel.getToolTipText();
        Desktop d = Desktop.getDesktop();
        if (Desktop.isDesktopSupported()) {
            try {
                d.browse(new URI(acriel));
            } catch (URISyntaxException ex) {
                return;
            } catch (IOException ioex) {
                return;
            }
        }
    }//GEN-LAST:event_acrielLabelMouseClicked

    private void novaVisitaMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novaVisitaMenuItemActionPerformed
        VisitaDialog dialog = new VisitaDialog(new javax.swing.JFrame(), "Visita - Funil de Vendas", true, loginDialog.getUsuarioSessao().getConsultor());
        dialog.setNovaVisita(true);
        dialog.setVisible(true);
}//GEN-LAST:event_novaVisitaMenuItemActionPerformed

    private void pesquisarVisitaMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarVisitaMenuItemActionPerformed
        VisitasDialog dialog = new VisitasDialog(new javax.swing.JFrame(), true, loginDialog.getUsuarioSessao().getConsultor());
        dialog.setVisible(true);
}//GEN-LAST:event_pesquisarVisitaMenuItemActionPerformed

    private void pesquisarPedidoMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarPedidoMenuItemActionPerformed
        PedidosConsultorDialog dialog = new PedidosConsultorDialog(this, true, loginDialog.getUsuarioSessao().getConsultor());
        dialog.setVisible(true);
    }//GEN-LAST:event_pesquisarPedidoMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //new MenuPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JLabel acrielLabel;
    private javax.swing.JMenu ajudaMenu;
    private javax.swing.JMenu arquivoMenu;
    private javax.swing.JPanel artePanel;
    private javax.swing.JLabel bemVindoLabel;
    private javax.swing.JMenu cadastroMenu;
    private javax.swing.JMenuItem calculadoraMenuItem;
    private javax.swing.JLabel dataLabel;
    private javax.swing.JMenu ferramentasMenu;
    private javax.swing.JMenu funilMenu;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPopupMenu.Separator jSeparator6;
    private javax.swing.JMenuItem logoutMenuItem;
    private javax.swing.JPanel logoutPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem novaVisitaMenuItem;
    private javax.swing.JMenuItem pesquisarPedidoMenuItem;
    private javax.swing.JMenuItem pesquisarVisitaMenuItem;
    private javax.swing.JMenuItem sairMenuItem;
    private javax.swing.JPanel usuarioPanel;
    // End of variables declaration//GEN-END:variables

    private void carregaCabecalho() {
        Usuario usuario = loginDialog.getUsuarioSessao();
        Date agora = new Date();
        SimpleDateFormat formatador = new SimpleDateFormat("EEEE, dd/MM/yyyy");
        dataLabel.setText("Hoje é " + formatador.format(agora));
        Timestamp ultimoAcesso = usuario.getUltimoAcesso();
        if (ultimoAcesso != null) {
            bemVindoLabel.setText("Bem-vindo " + usuario.getNome()
                    + ", seu último acesso foi em: " + DateUtils.formatarTimeStamp(ultimoAcesso));
        } else {
            bemVindoLabel.setText("Bem-vindo " + usuario.getNome()
                    + ", esse é seu primeiro acesso");
        }
        logoutPanel.add(this.getLogoutLabel());
        logoutMenuItem.setAction(logoutAction);
    }

    private void carregaRodape() {
        try {
            URL url = ClassLoader.getSystemResource("br/com/tbp/resources/rodape.png");
            imagem_rodape = new JImagePanel(ImageIO.read(url));
            imagem_rodape.setFillType(JImagePanel.FillType.RESIZE);
            artePanel.add(imagem_rodape);
            artePanel.repaint();
            artePanel.validate();
        } catch (IOException ioex) {
            System.out.println(ioex.getMessage());
        }
    }

    private class LogoutAction extends AbstractAction {

        public LogoutAction(String name) {
            putValue(Action.NAME, name);
            putValue(Action.SHORT_DESCRIPTION, name);
            putValue(Action.ACCELERATOR_KEY, javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        }

        public void actionPerformed(ActionEvent e) {
            dispose();
            loginDialog.setVisible(true);
        }
    }

    private JXHyperlink getLogoutLabel() {
        logoutAction = new LogoutAction("Realizar Logout");
        JXHyperlink logoutLink = new JXHyperlink(logoutAction);
        logoutLink.setFocusable(false);
        return logoutLink;
    }
}
